package com.ruoyi.teacher.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.teacher.domain.VO.TeacherVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teacher.mapper.teacherMapper;
import com.ruoyi.teacher.domain.teacher;
import com.ruoyi.teacher.service.IteacherService;
import java.util.Arrays;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
/**
 * 教师信息Service业务层处理
 * 
 * @author chenxl
 * @date 2025-04-08
 */
@Service
public class teacherServiceImpl implements IteacherService 
{
    @Autowired
    private teacherMapper teacherMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private ISysUserService userService;
    /**
     * 查询教师信息
     * 
     * @param uuid 教师信息主键
     * @return 教师信息
     */
    @Override
    public teacher selectteacherByUuid(String uuid)
    {
        return teacherMapper.selectteacherByUuid(uuid);
    }

    /**
     * 查询教师信息列表
     * 
     * @param teacher 教师信息
     * @return 教师信息
     */
    @Override
    public List<teacher> selectteacherList(teacher teacher)
    {
        return teacherMapper.selectteacherList(teacher);
    }

    /**
     * 新增教师信息
     * 
     * @param teacher 教师信息
     * @return 结果
     */
    @Override
    public int insertteacher(teacher teacher)
    {
        return teacherMapper.insertteacher(teacher);
    }

    /**
     * 修改教师信息
     * 
     * @param teacher 教师信息
     * @return 结果
     */
    @Override
    public int updateteacher(teacher teacher)
    {
        return teacherMapper.updateteacher(teacher);
    }

    /**
     * 批量删除教师信息
     * 
     * @param uuids 需要删除的教师信息主键
     * @return 结果
     */
    @Override
    public int deleteteacherByUuids(String[] uuids)
    {
        return teacherMapper.deleteteacherByUuids(uuids);
    }

    /**
     * 删除教师信息信息
     * 
     * @param uuid 教师信息主键
     * @return 结果
     */
    @Override
    public int deleteteacherByUuid(String uuid)
    {
        return teacherMapper.deleteteacherByUuid(uuid);
    }

    @Override
    public teacher selectteacherByteacherCode(String teacherId) {
        return teacherMapper.selectteacherByTeacherCode(teacherId);
    }

    @Override
    public List<teacher> selectTeacherByTeacherIds(ArrayList<String> strings) {
        return teacherMapper.selectTeacherByTeacherIds(strings);
    }

    /**
     * 导入数据
     *
     * @param teacherList 数据列表
     * @param updateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public String importteacherListroom(List<teacher> teacherList, Boolean updateSupport)
    {
        if (StringUtils.isEmpty(teacherList) || teacherList.size() == 0)
        {
            throw new RuntimeException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (teacher teacher : teacherList)
        {
            try
            {
                if (!updateSupport){
                    teacher.setUuid(IdUtils.fastSimpleUUID());
                    teacherMapper.insertteacher(teacher);
                    System.out.println("打印获取到的教师数据"+teacher);
                    SysUser user=createAccount(teacher);
                    System.out.println("打印获取到的user用户数据"+user);
                    userService.addUserWithValidation(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、教师 " + teacher.getName() + " 导入成功");
                }
                else
                {
                    teacherMapper.updateteacher(teacher);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、教师 " + teacher.getName() + " 更新成功");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、教师 " + teacher.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<TeacherVo> selectAllteacherList(TeacherVo teacherVo) {
        return teacherMapper.selectAllteacherList(teacherVo);
    }

    //根据学院ID列表查询对应的教师信息列表
    @Override
    public List<teacher> selectTeachersByCollegeIds(List<String> collegeIds) {
        if (collegeIds == null || collegeIds.size() == 0) {
            return Collections.emptyList();
        }
        return teacherMapper.selectTeachersByCollegeIds(collegeIds);
    }
    //根据教师实体类创建账号
    @Override
    public SysUser createAccount(teacher teacher) {
        SysUser user=new SysUser();
        user.setUserName(teacher.getTeacherCode());
        user.setNickName(teacher.getName());
        //密码为TeacherCode后六位
        user.setPassword(teacher.getTeacherCode().substring(teacher.getTeacherCode().length()-6));
        //性别男为1，女为2，教师表中存放为男、女转化为1、2存储
        if (teacher.getSex().equals("男")){
            user.setSex("1");
        }
        else if (teacher.getSex().equals("女")){
            user.setSex("2");
        }
        String deptId =teacherMapper.selectDeptIdByTeacherCode(teacher.getTeacherCode());
        System.out.println("打印获取到的deptId"+deptId);
        user.setDeptId(Long.valueOf(deptId));
        //List<SysRole> roleList = new ArrayList<>();
        SysRole role =roleService.selectRoleById(100L);
        System.out.println("----打印获取到的role----"+role);
        Long[] roleIds =new Long[]{role.getRoleId()};
        System.out.println("----打印获取到的roleIds---" + Arrays.toString(roleIds));
        user.setRoleIds(roleIds);
        return user;
    }
}
